//
//  FCommentListViewController.m
//  jingdongfang
//
//  Created by 郝志宇 on 16/9/12.
//  Copyright © 2016年 ZhiYu Hao. All rights reserved.
//

#import "FCommentListViewController.h"
#import "FCommentCell.h"
#import "XiangQingViewController.h"
#import "IGCommentMeApi.h"
#import "IGMeCommentApi.h"
#import "IGTopicDetailVC.h"
#import "ZTXQViewController.h"

@interface FCommentListViewController (){
    NSMutableArray *dataArray;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)IGCommentMeApi *comMeApi;
@property(nonatomic,strong)IGMeCommentApi *meComeApi;
@property(nonatomic,strong)NSMutableArray *msgArray;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, copy) NSString *lastTimeDate;

@end

@implementation FCommentListViewController
#pragma mark -懒加载
-(NSMutableArray *)msgArray{
    if (!_msgArray) {
        _msgArray=[NSMutableArray array];
    }
    return _msgArray;
}
#pragma mark -生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (_type == 0) {
//        self.title = @"评论我的";
        self.title = NSLocalizedString(@"Comments Received", nil);
    } else {
//        self.title = @"我评论的";
        self.title = NSLocalizedString(@"Comments Given", nil);
    }
    self.tableView.backgroundColor=[UIColor c2Color];
    
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 100;  //  随便设个不那么离谱的值
    [_tableView registerNib:[UINib nibWithNibName:@"FCommentCell" bundle:nil] forCellReuseIdentifier:@"FCommentCell"];
    _tableView.tableFooterView = [UIView new];
    _page = 1;
    _lastTimeDate = @"";
    if (_type==0) {//评论我的
        self.comMeApi = [IGCommentMeApi new];
        [self requestData1];
    }else{//我评论的
        self.meComeApi=[IGMeCommentApi new];
        [self requestData2];
    }
    [_tableView ig_setHeaderWithRefreshingBlock:^{
        _page = 1;
        _lastTimeDate = @"";
        if (_type==0) {//评论我的
            [self requestData1];
        }else{//我评论的
            [self requestData2];
        }
    }];
}

- (void)requestData1 {
    _comMeApi.pageNum = _page;
    _comMeApi.lastTimeDate = _lastTimeDate;
    @KWeakObj(self)
    [self.comMeApi startWithDecodeClass:[IGCommentMeResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [selfWeak.tableView ig_endHeaderRefreshing];
        [selfWeak.tableView ig_endFooterRefreshing];
        if (responseObject) {
            IGCommentMeResp *resp=responseObject;
            IGCommentMeModel *model=resp.data;
            if (model.msgs&&model.msgs.count>0) {
                [selfWeak setupFooter];
            }
            else
            {
                [_tableView ig_endFooterRefreshingWithNoMoreData];
            }
            _lastTimeDate = model.lastTimeDate;
            if (_page == 1) {
                _msgArray = [NSMutableArray arrayWithArray:model.msgs];
            } else {
                [_msgArray addObjectsFromArray:model.msgs];
            }
            [self.tableView reloadData];
        }
        else
        {
            //空白页处理逻辑写在这里
        }
    }];
    
}

-(void)requestData2 {
    _meComeApi.pageNum = _page;
    _meComeApi.lastTimeDate = _lastTimeDate;
    @KWeakObj(self)
    [self.meComeApi startWithDecodeClass:[IGMeCommentResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [selfWeak.tableView ig_endHeaderRefreshing];
        [selfWeak.tableView ig_endFooterRefreshing];
        if (responseObject) {
            IGMeCommentResp *resp=responseObject;
            IGMeCommentModel *model=resp.data;
            if (model.msgs&&model.msgs.count>0) {
                [selfWeak setupFooter];
            }
            else
            {
                [_tableView ig_endFooterRefreshingWithNoMoreData];
            }
            _lastTimeDate = model.lastTimeDate;
            if (selfWeak.page == 1) {
                _msgArray = [NSMutableArray arrayWithArray:model.msgs];
            } else {
                [_msgArray addObjectsFromArray:model.msgs];
            }
            [selfWeak.tableView reloadData];
        }
    }];
}
- (void)setupFooter
{
    @KWeakObj(self)
    [self.tableView ig_setFooterWithRefreshingBlock:^{
        _page++;
        if (_type==0) {//评论我的
            [selfWeak requestData1];
        }else{//评论我的
            [selfWeak requestData2];
        }

    }];
}
#pragma mark - UITableViewDelegate UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _msgArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FCommentCell *cell =[tableView dequeueReusableCellWithIdentifier:@"FCommentCell"];
    CommentMeModel *fCommentModel = _msgArray[indexPath.row];
    cell.fCommentModel = fCommentModel;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CommentMeModel *fCommentModel = _msgArray[indexPath.row];
    if ([fCommentModel.type isEqualToString:@"1"]) { //1评论作品
        XiangQingViewController * vc= [[XiangQingViewController alloc]init];
        vc.p_id =fCommentModel.topicId;
        vc.isRoot = YES;
        [UIViewController ig_pushViewController:vc animated:YES];
    } else if ([fCommentModel.type isEqualToString:@"2"]) {//2评论话题
        IGTopicDetailVC *vc = [[IGTopicDetailVC alloc] initWithTopicId:fCommentModel.topicId];
        [UIViewController ig_pushViewController:vc animated:YES];
    } else if ([fCommentModel.type isEqualToString:@"3"]) {//3评论专题
        ZTXQViewController *vc = [[ZTXQViewController alloc] init];
        vc.s_id = fCommentModel.topicId;
        [UIViewController ig_pushViewController:vc animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
